/*
 * Copyright (c) 2012 free soft workgroup
 * All rights reserved
 *
 */
#ifndef HTTP_H_
#define HTTP_H_

#include "utils.h"
#include "security.h"
/*
*REQUEST_METHOD
*/
enum REQUEST_METHOD{METHOD_GET,METHOD_POST,METHOD_HEAD,DESCRIBE,OPTIONS, SETUP,PLAY, TEARDOWN,RECORD,ANNOUNCE,UNKNOW};/*request methods*/

/*
*meta mime type
*/
enum   HTTP_MIME{HTML,HTM,FLV,FHV,MP4,MP3,AVI,ASF,OGG,TS,FFM,RTP,SDP,M3U8,UDPTS,HTB,INFO,XML,HB,SB,ASPNET,NONE};
/*
*http result status
*/
enum HTTP_STATUS{CONNECTCLOSE,KEEPALIVE,RESPONSEACCEPTED,RESPONSEPART,RESPONSEOK,RESPONSEBAD};/*HTTP status code*/

/*
*http request header define
*/
static const size_t MAX_REQ_LEN = 5120; /* Max request size     5KB     */
static const size_t MAX_REQ_PATH_LEN = 4096; /* Max request http get path size  4KB     */
static const size_t MIN_REQ_LEN = 16; /* Min request size 16B   */

/*
 * This guy holds parsed HTTP headers
 */
#pragma pack()
struct http_request_header
{
        REQUEST_METHOD method;  /*request method*/
        HTTP_MIME mime;/*response header mime type*/
        HTTP_STATUS httpstatus;/*HTTP_STATUS*/
        char path[MAX_REQ_PATH_LEN];/*Request path*/
        char file_name[MAX_REQ_PATH_LEN];/*file name ,or opration cmd Name,(all the data communicate with the http protocol,so the filename is not only the file'name)*/
        char pars[MAX_REQ_PATH_LEN];/*request pars*/
        char privat[MAX_REQ_PATH_LEN];/*private pars*/
        size_t  path_len;/*path length*/
        size_t  file_name_len;/*file_name length*/
        size_t  pars_len;/*request pars length*/
        size_t  privat_len;/*private pars length*/
        size_t  header_len;/*reqeust header length*/
        uint64_t  content_length;/*data length*/
        uint64_t  content_offset;/*reqeust data length offset*/
        uint64_t  content_end;/*reqeust data offset-end*/
        bool      is_have_rang; /*is have get rang flag*/
        /*
        *url parameter set
        */
        int id;/*content id*/
        int opcode; /*opration cmd code*/
        int seek_time;/*seek time*/
        uint64_t seek_offset;/*seek offset*/
        /*
        *request prase result
        */
        int     request_parse_result;
};
/*
 *http utils function
 *
 */
class http
{
public:
        http(void);
        ~http(void);

        /*
        * init request_header
        */
        static void init_request_header(http_request_header *request_header);
        /*
        * parse request header
        */
        static int parse_request_header(http_request_header *request_header,char * buf,size_t size,int & errorcode);
        /*
        * parse response header
        */
        static size_t parse_response_header(char * buf,int size,int &httpcode,uint64_t &Contentlength);

        /*
        *struct response header
        */
        static size_t struct_response_header(char * buf,HTTP_MIME mime_type,uint64_t content_len);
        /*
        *struct response header
        */
        static size_t struct_response_header(char * buf,HTTP_MIME mime_type,uint64_t offset,uint64_t end,uint64_t content_length);
        /*
        *struct response header
        */
        static size_t struct_response_header(char * buf,HTTP_MIME mime_type,int code = 200);
        /*
        *struct response header by data
        */
        static size_t struct_response_header(char * buf,HTTP_MIME mime_type,const char * data);
        /*
		*struct response redirect header
		*/
		static size_t struct_response_redirect_header(char * buf,const char * location);
        /*
         *struct crossxml doma response header
         */
        static size_t struct_crossxml_doma_response_header(char * buf);
        /*
        *struct request header
        */
        static size_t struct_request_header(char * buf,size_t size,const char * path,const char * host,const char * privt,uint64_t offset = 0,uint64_t end = 0,uint64_t content_length = 0);

        /*
        * get_reqeust_method
        */
        static REQUEST_METHOD get_reqeust_method(char * buf,size_t size);
        /*
        * get data ling lenght eg:\r\n
        */
        static size_t get_line_len(char * buf,size_t size);
        /*
        * anlysis http_request_header::path
        */
        static int anlysis_path(http_request_header *request_header);
        /*
        * anlysis HTTP_MIME bye file name
        */
        static HTTP_MIME anlysis_mime_type(http_request_header *request_header);
        /*
        * anlysis http_request_header::rang
        */
        static int anlysis_rang(http_request_header *request_header,char * rang,size_t len);

        /*
        *get other request url pras
        * find par value by name
        * return value length
        */
        static size_t get_url_pars_value(http_request_header * hrt,const char * name, char * buf, size_t buf_len);

private:

};

#endif  //HTTP_H_
